package com.zhang.framelib.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日期处理类
 * Title:DateUtil
 * Description:
 * @Edit_Description:cuishuaiming
 * @version:zhang-framelib 1.0
 */
public class DateUtils {
	
	public static String dateFormat = "yyyy-MM-dd";
    public static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat ZERO_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    public static final SimpleDateFormat FINAL_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 日期格式中的特殊字符.
     */
    public static final String[][] DATE_SPECIAL_STRS = { { "．", "-" }, { "/", "-" }, { "／", "-" }, { ".", "-" },
            { "：", ":" }, { "——", "-" }, { "－", "-" }, { "年", "-" }, { "月", "-" }, { "日", "" } };
    /**
     * 全角空格字符.
     */
    public static final String SBC_BLANK = "　";
    /**
     * 半角空格字符.
     */
    public static final String DBC_BLANK = " ";
    
    /**
     * Excel读取日期为数值（从1899-12-30到当前日期的天数值）.
     */
    public static final Calendar CAL = Calendar.getInstance();
    
    public static final int BEGIN_YEAR = 1899;
    public static final int MONTH_DAY = 30;
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    
    /**
     * 默认起始时间
     */
    static {
        CAL.set(Calendar.YEAR, BEGIN_YEAR);
        CAL.set(Calendar.MONTH, Calendar.DECEMBER);
        CAL.set(Calendar.DAY_OF_MONTH, MONTH_DAY);
        CAL.set(Calendar.HOUR_OF_DAY, 0);
        CAL.set(Calendar.MINUTE, 0);
        CAL.set(Calendar.SECOND, 0);
        CAL.set(Calendar.MILLISECOND, 0);
    }
    
//    /**
//     * ？？？   不知道这里使用多线程的理由
//     */
//    private static ThreadLocal<Map<String, DateFormat>> threadLocal;
//    
//    static{
//    	synchronized (DateUtils.class) {
//			threadLocal = new ThreadLocal<Map<String, DateFormat>>();
//		}
//    }
    
    /**
     * Excel读取日期为数值（从1899-12-30到当前日期的天数值）最大支持的日期：2300-12-31.
     */
    public static final int MAX_DAY_1899_12_30 = 146463;
    
    /** 秒和毫秒的转换进度.*/
    public static final int SECOND_MILLISECOND = 1000;
    /** 天和小时的转换进度.*/
    public static final int DAY_HOUR = 24;
    /** 小时和秒的转换进度.*/
    public static final int HOUR_SECOND = 3600;
    
    public static final int STR_LEN4 = 4;
    public static final int STR_LEN6 = 6; 
    public static final int STR_LEN8 = 8;
    public static final int STR_LEN9 = 9; 

    /**
     * 返回当前日期 Date.
     */
    public static Date getCurrentDate() {
        return Calendar.getInstance().getTime();
    }
    
    /**
     * 获取增加num天后的日期
     * @param date  原来的时间
     * @param num   增加的天数
     * @return
     * @Edit_Description:
     * @Create_Version:zhang-framelib 1.0
     */
    @SuppressWarnings("static-access")
    public static Date addDay(Date date, int num){
    	
    	Calendar calendar = new GregorianCalendar();
    	calendar.setTime(date);
    	calendar.add(calendar.DAY_OF_MONTH, num);
    	date = calendar.getTime();
    	
    	return date;
    }
    
    /**
     * 将日期转换为字符串
     * @param date   字符串格式的日期
     * @param pattern  期望转化的格式  默认为 "yyyy-MM-dd"
     * @return
     * @Edit_Description:
     * @Create_Version:zhang-framelib 1.0
     */
    public static Date parseDate(String date, String pattern){
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern == null? dateFormat : pattern);
    	
    	try {
			return sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("字符串{}不能转换为格式",date,pattern);
		}
    	
    	return null;
    }
}
